package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.RegionQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list() {
        return "region/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Region entity) {
        regionService.saveOrUpdate(entity);
        return "redirect:/region/list.do";
    }

    /*
        如果返回treeview用的数据格式[{text:xx,tags:['xx']}]
     */
    @RequestMapping("/queryTreeview")
    @ResponseBody
    public Object queryTreeview(RegionQueryObject qo) {
        List<Region> list = regionService.queryMenus(qo);
        /*List<Map<String, Object>> treeviews = new ArrayList<>();
        for (Region region : list) {
            Map<String, Object> treeview = region.toTreeview();
            treeviews.add(treeview);
        }*/
        System.out.println(list);
        if (qo.isTreeview()) {
            return list.stream().map(Region::toTreeview).collect(Collectors.toList());
        }
        return list;
    }

}
